package dr.board.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dr.board.dao.BoardDAO;
import dr.controller.Action;

public class ListAction implements Action {

    public String execute(HttpServletRequest request,
        HttpServletResponse response)throws Throwable {
        
        // 검색필드 & 검색어 처리
    	request.setCharacterEncoding("EUC-KR");
    	String keyField = request.getParameter("keyField");
    	String keyWord = request.getParameter("keyWord");
    	if(keyField == null) {
    		keyField = "";
    	}
    	if(keyWord == null){ 
    		keyWord = "";
    	}
    	
        String pageNum = request.getParameter("pageNum");//페이지 번호

        if (pageNum == null) {
            pageNum = "1";
        }
        int pageSize = 10;//한 페이지의 글의 개수
        int currentPage = Integer.parseInt(pageNum);
        int startRow = (currentPage - 1) * pageSize + 1;
        int endRow = currentPage * pageSize;
        int count = 0;
        int number=0;

        List articleList = null;
        BoardDAO dbPro = BoardDAO.getInstance();
        count = dbPro.getArticleCount(keyField, keyWord);
        
        if (count > 0) {
            articleList = dbPro.getArticles(startRow, endRow, keyField, keyWord);//현재 페이지에 해당하는 글 목록
        }

		number=count-(currentPage-1)*pageSize;

        request.setAttribute("currentPage", new Integer(currentPage));
        request.setAttribute("startRow", new Integer(startRow));
        request.setAttribute("endRow", new Integer(endRow));
        request.setAttribute("count", new Integer(count));
        request.setAttribute("pageSize", new Integer(pageSize));
        request.setAttribute("keyField", keyField);
        request.setAttribute("keyWord", keyWord);
		request.setAttribute("number", new Integer(number));
        request.setAttribute("articleList", articleList);
        
        return "/board/list.jsp";//해당 뷰
    }
}
